define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        utils = require('js/utils/tools'),
        helper = require('js/apps/report/common');

    var ChnLinkView = Backbone.View.extend({
        events: {
            'change #platform': 'changePlatform',
            'click #addDebug': 'addDebug',
            'click #saveDebug': 'saveDebug',
            'click a.onClickDeleteDebug': 'deleteDebug',
        },
        initialize: function (options) {
            this.options = options;
            this.chnLinks = [];
        },
        changePlatform: function (e) {
            this.options.platform = $('#platform').val();
            this.load();
        },
        addDebug: function (e) {
            var tr = $('<tr></tr>');
            tr.append('<td><input type="text" class="form-control" value="" required></td>');
            tr.append('<td><input type="text" class="form-control" value="" required></td>');
            tr.append('<td><a class="glyphicons glyphicons-remove_2 onClickDeleteDebug" href="javascript:;"></a></td>');
            $('.debugTbody').append(tr);
        },
        deleteDebug: function (e) {
            $(e.target).parent().parent().remove();
        },
        saveDebug: function (e) {
            var chns = {};
            $('.debugTbody tr').each(function(i) {
                var cells = $(this).find("td");
                var chn = $(cells[0]).find("input").val().trim();
                var link_url = $(cells[1]).find("input").val().trim();
                chns[chn] = link_url;
            });
            $.post('/admin/market/set/'+this.options.platform+'/', JSON.stringify(chns)).done(function (resp) {
                var data = resp.data;
                notify.success('提示', '保存成功！');
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                chnLinks: this.chnLinks,
                options: this.options,
            }));
            return this;
        },
        load: function () {
            var that = this;
            $.get('/admin/market/get/'+this.options.platform+'/').done(function (resp) {
                that.chnLinks = resp.data.list;
                that.renderWithData();
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "preset/ChnLink.html",
        render: function () {
            var view = new ChnLinkView({
                platform: "android",
            });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});
